using ECETCentralController.Communication;
using LitJson;
using Mono.Cecil;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ShaPanContent : MonoBehaviour
{

    [SerializeField] GameObject Item;
    [SerializeField] GameObject ItemParent;
    [SerializeField] Button backBtn;

    private string Ip = "192.168.10.151";
    private int port = 17001;
    private Data datas;

    private bool nextState = false;
    private bool isBack = false;
    private Data lastData;
    void Start()
    {
        var Info = Resources.Load<TextAsset>("Table/Info");
        var json = Info.text;
        datas = JsonMapper.ToObject<Data>(json);

        backBtn.onClick.RemoveAllListeners();
        backBtn.onClick.AddListener(() => {

            var curParent = FindParentNode(lastData, datas);
            //var dt = FindParentNode(curParent, datas);
            
            nextState = false;
            SendMsg(curParent);
            CreateItem(curParent);

        });

        CreateItem(datas);

    }

    private void CreateItem(Data curdata)
    {
        lastData = curdata;
        if (curdata.childs.Count <= 0) return;
        DestroyChild(ItemParent);
        nextState = curdata.childs.Count > 0;
        for (int i = 0; i < curdata.childs.Count; i++)
        {
            GameObject go = Instantiate(Item);
            AddChild(go, ItemParent);
            UpdataItemInfo(go, curdata.childs[i]);
        }
    }

    private void UpdataItemInfo(GameObject go,Data data)
    {
        var des = go.transform.Find("des").GetComponent<Text>();
        des.text = data.mediaName.Split('.')[0];
        var btn = go.GetComponent<Button>();

        isBack = false;
        btn.onClick.AddListener(() => {

            lastData = data;
            SendMsg(data);
            nextState = data.childs.Count > 0;
            if (nextState)
            {
                CreateItem(data);              
            }
            isBack = !nextState;
        });
    }

    private void SendMsg(Data data)
    {
        SendData sendData;
        sendData.fadeItemIndex = data.fadeItemIndex;
        sendData.mediaName = data.mediaName;
        sendData.mediaType = data.mediaType;
        sendData.whetherHighLight = false;
        var json = JsonMapper.ToJson(sendData);
        Debug.Log($"{data.fadeItemIndex} {data.mediaName}  {data.mediaType} {false}");
        UDPSender.SendString(Ip, port, json);
    }

    private Data FindParentNode(Data targetNode, Data rootNode)
    {
        if (targetNode == rootNode) return rootNode;
        if (rootNode == null || targetNode == null)
        {
            return null;
        }

        if (rootNode.childs.Contains(targetNode))
        {
            return rootNode;
        }

        foreach (var child in rootNode.childs)
        {
            var parent = FindParentNode(targetNode, child);
            if (parent != null)
            {
                return parent;
            }
        }

        return null;
    }


    public void DestroyChild(GameObject go)
    {
        for (int i = 0; i < go.transform.childCount; i++)
        {
            go.transform.GetChild(i).gameObject.SetActive(false);
            GameObject.Destroy(go.transform.GetChild(i).gameObject);
        }
    }

    public void AddChild(GameObject item, GameObject parent)
    {
        item.transform.SetParent(parent.transform);
        item.transform.localScale = Vector3.one;
        item.transform.localPosition = Vector3.zero;
        item.SetActive(true);
    }

    void Update()
    {
        
    }
}

public struct SendData
{
    public int fadeItemIndex;
    public string mediaName;
    public int mediaType;
    public bool whetherHighLight;
}

public class Data
{
    public int fadeItemIndex;
    public string mediaName;
    public int mediaType;
    public List<Data> childs = new List<Data>();
}
